package com.dz.goods.controller;

import com.dz.common.base.BaseApiService;
import com.dz.common.base.ResponseBase;
import com.dz.common.core.exception.NotFoundDataException;
import com.dz.goods.domain.entity.Goods;
import com.dz.goods.domain.entity.Spec;
import com.dz.goods.domain.entity.Spu;
import com.dz.goods.service.SpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName : SpuController
 * @Description :
 * @Author : ming
 * @Date: 2020-12-04 13:27
 **/
@RestController
@RequestMapping("/goods/spu")
@Api(tags = "SPU管理")
public class SpuController extends BaseApiService {
    @Autowired
    private SpuService spuService;

    @PostMapping("/add")
    @ApiOperation(value = "添加SPU")
    public ResponseBase add(@RequestBody Spu spu) throws Exception {
        Spu add = spuService.add(spu);
        return setResultSuccess(add) ;
    }
    @PostMapping("/edit")
    @ApiOperation(value = "更新SPU")
    public ResponseBase edit(@RequestBody Spu spu) throws Exception {
        Spu edit = spuService.edit(spu);
        return setResultSuccess(edit) ;
    }
    @GetMapping("/get_by_id")
    @ApiOperation(value = "根据id查询SPU")
    public ResponseBase getSpuById(Long spuId) throws Exception {
        Spu spu = spuService.findById(spuId);
        return setResultSuccess(spu) ;
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除SPU")
    public ResponseBase delete(Long spuId) throws Exception {
        spuService.delete(spuId);
        return setResultSuccess();
    }
    @PostMapping("/save")
    @ApiOperation(value = "保存商品")
    public ResponseBase save(@RequestBody Goods goods) throws Exception {
        spuService.saveGoods(goods);
        return setResultSuccess("保存商品成功");
    }
    @GetMapping("/get_goods_by_id")
    @ApiOperation(value = "根据id查询商品")
    public ResponseBase getGoodsById(Long spuId) throws Exception {
        Goods goods = spuService.findGoodsById(spuId);
        return setResultSuccess(goods) ;
    }
    @PostMapping("/on_shelf")
    @ApiOperation(value = "上架商品")
    public ResponseBase onShelf(@RequestParam Long spuId) throws NotFoundDataException {
        spuService.onShelf(spuId);
        return setResultSuccess("上架商品成功");
    }
    @PostMapping("/off_shelf")
    @ApiOperation(value = "下架商品")
    public ResponseBase offShelf(@RequestParam Long spuId) throws NotFoundDataException {
        spuService.offShelf(spuId);
        return setResultSuccess("下架商品成功");
    }
}
